package service.impl.v2;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import dao.v2.OrderVOMapper;
import dao.v2.PayVOMapper;
import dao.v2.UorderPayVOMapper;
import model.LogBuyCashVO;
import model.LogEarnVO;
import model.LogOrder;
import model.MemberVO;
import model.OrderVO;
import service.v2.UorderPayService;

@Service
public class UorderPayServiceImpl implements UorderPayService {

	@Autowired
	private OrderVOMapper orderVOMapper;
	
	@Autowired
	private UorderPayVOMapper dao;
	
	@Autowired
	private PayVOMapper payVOMapper;
	
	
	/**
	 *  订单支付(余额购物专用金)
	 * 
	 */
	public boolean dealPayOrderBalaceCash(String[] order_id_List, String pay_type, MemberVO memberVO,String total_price) {
		
		//付款时间倒计时暂停
		orderVOMapper.stopOrderPayTimerByPayNo(order_id_List);
 		//订单数量
 		int orderCount = orderVOMapper.getOrderCount(order_id_List,memberVO.getId());
 		OrderVO order  = orderVOMapper.getOrderById(order_id_List[0]);
 		//扣钱
		int payCount = dao.updMemberCash(pay_type,total_price,memberVO.getId(),orderCount,order_id_List,order.getCountry());
		if(payCount>0){
			 orderVOMapper.updorderPay(pay_type,order_id_List,memberVO.getId());
			 dao.updOrderCase(order_id_List);
			 
			 if("3".equals(pay_type)){
				 for (int i = 0; i < order_id_List.length; i++) {
						OrderVO order1  = orderVOMapper.getOrderById(order_id_List[i]);
						LogEarnVO log  = new LogEarnVO();
						log.setMember_id(memberVO.getId());
						log.setType("5");
						log.setState("2");
						log.setBill_type("1");
						log.setOrder_id(order_id_List[i]);
						log.setRemark("在线商城订单支付");
						log.setFree_cash(String.valueOf(Double.parseDouble(order1.getTotal_price())+Double.parseDouble(order1.getFreight())));
						log.setRead_yn("N");
						log.setCountry(order1.getCountry());
						log.setRate_cash("1");
						payVOMapper.addLogEarn(log);
				}
			 }/*else if("4".equals(pay_type)){
				 for (int i = 0; i < order_id_List.length; i++) {
						OrderVO order1  = orderVOMapper.getOrderById(order_id_List[i]);
						LogBuyCashVO  log = new LogBuyCashVO();
						log.setMember_id(memberVO.getId());
						log.setType("3");
						log.setBill_type("1");
						log.setState("2");
						log.setBuy_cash(String.valueOf(Double.parseDouble(order1.getTotal_price())+Double.parseDouble(order1.getFreight())));
						log.setOrder_id(order_id_List[i]);
						log.setRemark("在线商城订单支付");
						payVOMapper.addLogBuyCash(log);
				}
			 }*/
			 
			 dao.delTimerByOrderIdList(order_id_List);
			 //留LOG
			 for (int i = 0; i < order_id_List.length; i++) {
					LogOrder log = new LogOrder();
					log.setOrder_id(order_id_List[i]);
					log.setMember_id(memberVO.getId());
					log.setRemark("成功付款");
					orderVOMapper.InsertLogOrder(log);
			 }
			 return true;
		}else{
			return false;
		}
	}

	/**
	 * 支付宝微信 付款以后修改 订单包裹数据
	 */
	public void updOrderAndCaseAfterUnnormalPay(String pay_type, String conn_pay_id, List<String> order_id_array) {
		dao.updOrderAndCaseAfterUnnormalPay(pay_type,conn_pay_id,order_id_array);
	}
	
}
